home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Buttons and Labels and Scrolls (Oh, My!) / AutoScaleDemo / AutoScaleDemo.cs next >
Encoding:
Text File  |  2001-01-15  |  1.7 KB  |  51 lines

  1. //--------------------------------------------
  2. // AutoScaleDemo.cs ⌐ 2001 by Charles Petzold
  3. //--------------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Windows.Forms;
  7.  
  8. class AutoScaleDemo: Form
  9. {
  10.      public static void Main()
  11.      {
  12.           Application.Run(new AutoScaleDemo());
  13.      }
  14.      public AutoScaleDemo()
  15.      {
  16.           Text = "Auto-Scale Demo";
  17.           Font = new Font("Arial", 12);
  18.           FormBorderStyle = FormBorderStyle.FixedSingle;
  19.  
  20.           int[] aiPointSize = { 8, 12, 16, 24, 32 };
  21.  
  22.           for (int i = 0; i < aiPointSize.Length; i++)
  23.           {
  24.                Button btn    = new Button();
  25.                btn.Parent    = this;
  26.                btn.Text      = "Use " + aiPointSize[i] + "-point font";
  27.                btn.Tag       = aiPointSize[i];
  28.                btn.Location  = new Point(4, 16 + 24 * i);
  29.                btn.Size      = new Size(80, 16);
  30.                btn.Click    += new EventHandler(ButtonOnClick);
  31.           }
  32.           ClientSize = new Size(88, 16 + 24 * aiPointSize.Length);
  33.           AutoScaleBaseSize = new Size(4, 8);
  34.      }
  35.      protected override void OnPaint(PaintEventArgs pea)
  36.      {
  37.           pea.Graphics.DrawString(Text, Font, 
  38.                                   new SolidBrush(ForeColor), 0, 0);
  39.      }
  40.      void ButtonOnClick(object obj, EventArgs ea)
  41.      {
  42.           Button btn = (Button) obj;
  43.  
  44.           SizeF sizefOld = GetAutoScaleSize(Font);
  45.           Font = new Font(Font.FontFamily, (int) btn.Tag);
  46.           SizeF sizefNew = GetAutoScaleSize(Font);
  47.  
  48.           Scale(sizefNew.Width  / sizefOld.Width,
  49.                 sizefNew.Height / sizefOld.Height);
  50.      }
  51. }